<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form v-loading="formLoading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="设备序列号" prop="serialNum">
      <el-input v-model="dataForm.serialNum" placeholder="设备序列号"></el-input>
    </el-form-item>
    <el-form-item label="所属田块" prop="farmlandInfoId">
      <el-select style="width:100%"
            v-model="dataForm.farmlandInfoId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键字"
            :remote-method="remoteMethod"
            :loading="farmlandListLoading"
            v-loadmore="farmlandListLoadMore">
            <el-option
            v-for="item in farmlandList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" v-loading="formLoading" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        formLoading: false,
        dataForm: {
          id: 0,
          farmlandInfoId: '',
          serialNum: '',
          password: '',
          remark: ''
        },
        farmlandListLoading: false,
        farmlandList:[],// 处理人列表
        farmlandListParams:{},// 处理人查询参数
        dataRule: {
          farmlandInfoId: [
            { required: true, message: '所属田块不能为空', trigger: 'blur' }
          ],
          serialNum: [
            { required: true, message: '设备序列号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ]
         
        }
      }
    },
    methods: {
      init (id) {
        // 加载田块下拉框
        this.farmlandListLoadMore('init');

        this.dataForm.id = id || 0
        this.visible = true
        this.formLoading = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrtuinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.farmlandInfoId = data.bussRtuInfo.farmlandInfoId
                this.dataForm.serialNum = data.bussRtuInfo.serialNum
                this.dataForm.password = data.bussRtuInfo.password
                this.dataForm.remark = data.bussRtuInfo.remark
                this.dataForm.version = data.bussRtuInfo.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid && !this.formLoading) {
            this.formLoading = true
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrtuinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'farmlandInfoId': this.dataForm.farmlandInfoId,
                'serialNum': this.dataForm.serialNum,
                'password': this.dataForm.password,
                'version': this.dataForm.version,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.formLoading = false
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 下拉加载框查询
      remoteMethod(query) {
        if (query !== '') {
          this.farmlandListLoadMore('query',query)
        }
      },
      // 这里写入滚动加载要触发的方法
      farmlandListLoadMore (type,query) {
        this.farmlandListLoading = true

        if(type && (type == 'init' || type == 'query')){
            this.farmlandListParams.page = 1
            this.farmlandList = []
        }else if(this.farmlandListParams.page){
            if(this.farmlandListParams.page>=this.farmlandListParams.totalPage){
                this.farmlandListLoading = false
                return
            }
            this.farmlandListParams.page += 1
        }

        var _this = this
        this.$http({
            url: this.$http.adornUrl('/buss/bussfarmlandinfo/list'),
            method: 'get',
            params: this.$http.adornParams({
                "name": query, //搜索参数
                "page": _this.farmlandListParams.page || 1, //分页参数
                "limit": _this.farmlandListParams.limit || 15, //每次查询10条记录
            })
        }).then(({data}) => {
            _this.farmlandListLoading = false
            _this.farmlandListParams.totalPage = data.page.totalPage
            _this.farmlandList = _this.farmlandList.concat(data.page.list)
        })
      }
    }
  }
</script>
